package cn.songit.controller;

import cn.songit.service.DemoService;
import cn.songit.service.DemoServiceJdbcTemplate;
import cn.songit.entity.Demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demo")
public class DemoController {
    @RequestMapping("getDemo")
    public Demo getDemo(){
        Demo demo = new Demo();
        demo.setId(1);
        demo.setName("Mxs");
        return demo;
    }

    @Autowired
    private DemoService demoService;

    @Autowired
    private DemoServiceJdbcTemplate demoServiceJdbcTemplate;

    @RequestMapping("/zeroException")
    public int zeroException(){
        return 100/0;
    }

    @ResponseBody
    @RequestMapping("/save")
    public String save(){
        demoService.save(new Demo("angle"));
        return "The data was saved.";
    }

    @ResponseBody
    @RequestMapping("/show")
    public Demo showDemo(@RequestParam(name="id", defaultValue = "1",required = true)Integer id){
        return demoServiceJdbcTemplate.getById(id);
    }
}
